package com.dingwen.treasure.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.core.util.TreeUtil;
import com.dingwen.treasure.manage.dto.SysDepartmentDto;
import com.dingwen.treasure.manage.vo.SysDepartmentForSelectVo;
import com.dingwen.treasure.manage.vo.SysDepartmentVo;
import com.dingwen.treasure.model.entity.mybatisplus.SysDepartment;
import com.dingwen.treasure.mybatisplus.controller.AbstractBaseControllerMybatisPlusImpl;
import com.dingwen.treasure.mybatisplus.service.SysDepartmentService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 　SysDepartmentController
 * 　@author dingwen
 * 　@date 2022/6/22
 */
@Api(tags = "部门列表,基于mybatis-plus封装BaseController")
@ApiSupport(author = "dingwen")
@Slf4j
@RestController
@RequestMapping("sys/department")
public class SysDepartmentController extends AbstractBaseControllerMybatisPlusImpl<SysDepartment, SysDepartmentDto,
        SysDepartmentVo> {

    /**
     * sysDepartmentService
     */
    @Resource
    private SysDepartmentService sysDepartmentService;


    /**
     * 获取不进行转换的部门树列表
     */
    @ApiOperation("获取不进行转换的部门树列表")
    @GetMapping("/tree")
    public Result<List<SysDepartment>> getTreeBySysDepartment() {
        List<SysDepartment> departments = getDepTreeOrder();
        List<SysDepartment> departmentsForTree = TreeUtil.builder(departments, 0L);
        return success(departmentsForTree);
    }

    /**
     * 获取进行转换的部门树列表
     *
     * @return {@link Result}<{@link List}<{@link SysDepartmentForSelectVo}>>
     */
    @GetMapping("/treeVO")
    @ApiOperation("获取进行转换的部门树列表")
    public Result<List<SysDepartmentForSelectVo>> getTreeBySysDepartmentForSelectVO() {
        List<SysDepartment> departments = getDepTreeOrder();

        List<SysDepartmentForSelectVo> treeVO = TreeUtil.builderAndConvert(departments, 0L,
                SysDepartmentForSelectVo.class, SysDepartment.class);
        return success(treeVO);
    }

    /**
     * 获取所有部门列表依据order number排序
     *
     * @return {@link List}<{@link SysDepartment}>
     */
    private List<SysDepartment> getDepTreeOrder() {
        LambdaQueryWrapper<SysDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(SysDepartment::getOrderNumber);
        return sysDepartmentService.list(wrapper);
    }
}
